/**
 * 
 */
package com.oet.xml;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;


/**
 *
 */
public class XmlUtility {
	
	private static XmlUtility instance = null;
	private static Logger log4j = Logger.getLogger(XmlUtility.class);
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	
	
	private XmlUtility(){
		super();
	}
	
	
	public DocumentBuilderFactory getFactory() {
		return factory;
	}


	public static XmlUtility getYdkXml() {
		if (instance == null) {
			instance = new XmlUtility();
			instance.getFactory().setNamespaceAware(true);
			instance.getFactory().setValidating(true);
		}
		return instance;
	}
	
	public DocumentBuilder getDocBuilder()
	{
		DocumentBuilder builder = null;
		try {
			 builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			log4j.error("parseXml could not get builder: " + e.getMessage());
			e.printStackTrace();
		}
		return builder;
	}
	
	public Document createNewDocument()
	{
		DocumentBuilder builder = getDocBuilder();
		if (builder != null)
		{
			return builder.newDocument();
		}
		return null;
	}
	
	public Document parseXml(String fileName)
	{
		
		DocumentBuilder builder = getDocBuilder();
		Document doc = null;
		if(builder != null)
		{
			
			try {
				doc = builder.parse(fileName);
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				log4j.error("parse Xml error: " + e.getMessage());
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log4j.error("parse Xml error: " + e.getMessage());
				e.printStackTrace();
			}
		}
		return doc;
	}
	
	public void saveXml(Document doc, String fileName, String encoding)
	{
		FileOutputStream outStream=null; 
		try {
			outStream = new FileOutputStream(fileName);
		} catch (Exception e) {
			System.err.println(e);
			System.exit(1);
		}
		OutputStreamWriter outWriter = null;
		try {
			outWriter = new OutputStreamWriter(outStream, encoding);
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (outWriter != null)
		{
			// doc.write(outWriter,"GB2312");
			callWriteXMLFile(doc, outWriter, encoding);
			try {
				outWriter.close();
				outStream.close();
			} catch (Exception e) {
				log4j.error("saveXml error: " + e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	private void callWriteXMLFile(Document doc,OutputStreamWriter w,String encoding) 
	{
		try{
			Source source=new DOMSource(doc); 
			Result ret=new StreamResult(w); 
			Transformer xformer=TransformerFactory.newInstance().newTransformer(); 
			xformer.setOutputProperty(OutputKeys.ENCODING,encoding); 
			xformer.transform(source,ret); 
		}catch(TransformerConfigurationException e) 
		{
			log4j.error("callWriteXMLFile error: " + e.getMessage());
			e.printStackTrace(); 
		} 
		catch(TransformerException e) 
		{
			log4j.error("callWriteXMLFile error: " + e.getMessage());
			e.printStackTrace(); 
		} 
	}
	

}
